<?php
namespace  app\admin\controller;

use app\admin\AdminCommon;
use think\Db;

class Brand extends AdminCommon
{
    public function getindex()
    {
        $data=Db::table('tp_brand')->alias('b')->field('b.id as id,c.id as cid,brand_name,brand_pic,state,cate_id,create_time,catname')->join('tp_category c','b.cate_id=c.id')->select();
        $i=0;
        foreach($data as $v)
        {
            $data[$i]['create_time']=date('Y-m-d',$v['create_time']);
            $data[$i]['brand_pic']='/uploads/brand_pic/'.$v['brand_pic'];
            $i++;
        }

        return $this->fetch('brand/index',['data'=>$data,'b'=>1]);
    }

    //品牌添加
    public function getadd()
    {
        $Category=new \app\admin\model\Category();
        $data=$Category->tree();
        $this->assign('data',$data);
        return $this->fetch('brand/create');
    }

    //处理品牌添加
    public function postdoadd()
    {
       $request=$this->request;
       $res=$request->param();
       $res['create_time']=time();
       unset($res['file']);
       unset($res['action']);
       $result=Db::table('tp_brand')->insert($res);
       if($result) {
            //判断插入是否成功
            $data=[
                'status'=>1,
                'msg'=>'添加成功',
            ];
        }else {
            $data=[
                'status'=>0,
                'msg' => '添加失败',
            ];
        };
        return $data;

     }

    //图片上传
    public function postpics()
    {
        $files = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($files)
        {
            $info = $files->validate(['size'=>307200,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/brand_pic');

            if($info){
                // 成功上传后 获取上传信息
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $brand_pic=$info->getSaveName();
                return json(array('status'=>1,'path'=>$brand_pic,'msg'=>'图片上传成功'));
            }else{
                // 上传失败获取错误信息
                echo $info->getError();
                return json(array('status'=>0,'msg'=>'上传失败'));
            }
        }
    }

    //品牌修改
    public function getedit()
    {
        $request=$this->request;
        $res=$request->param();
        $datas=Db::table('tp_brand')->where('id',$res['id'])->find();
       // echo '<pre>';
       //print_r($datas);
//        exit;
        $Category=new \app\admin\model\Category();
        $data=$Category->tree();
        $this->assign('data',$data);
        $this->assign('datas',$datas);
        return $this->fetch('brand/edit');
    }

    //处理品牌修改
    public function postdoedit()
    {
        $request=$this->request;
        $res=$request->param();
        $old_res=Db::table('tp_brand')->where('id',$res['id'])->find();
        $old_pic=$old_res['brand_pic'];
        $res['create_time']=time();
        unset($res['action']);
        unset($res['file']);
        //判断他没有上传新的图片
        if($res['brand_pic']==$old_pic)
        {
            $result=Db::table('tp_brand')->where('id',$res['id'])->update($res);
            if($result) {
                //判断插入是否成功
                $data=[
                    'status'=>1,
                    'msg'=>'修改成功',
                ];
            }else {
                $data=[
                    'status'=>0,
                    'msg' => '修改失败',
                ];
            };
        }else{
            //有图片上传
            $path='./uploads/brand_pic/'.$old_pic;
            unlink($path);
            $result=Db::table('tp_brand')->where('id',$res['id'])->update($res);
            if($result) {
                //判断插入是否成功
                $data=[
                    'status'=>1,
                    'msg'=>'修改成功',
                ];
            }else {
                $data=[
                    'status'=>0,
                    'msg' => '修改失败',
                ];
            };
        }
        return $data;
    }

    public function postdelete()
    {
        $request=$this->request;
        $res=$request->param();
        $new_res=Db::table('tp_brand')->where('id',$res['id'])->find();
        $pathinfoss=$new_res['brand_pic'];
        $pathinfoes='./uploads/brand_pic/'.$pathinfoss;
        unlink($pathinfoes);
        $paths=pathinfo($pathinfoes,PATHINFO_DIRNAME);
        if(count(scandir($paths))==2)
        {
            rmdir($paths);
        }
        $result=Db::table('tp_brand')->where('id',$res['id'])->delete();
        if($result){
            $data=[
                'status'=>1,
                'msg'=>'删除成功',
            ];
        }else{
            $data=[
                'status'=>0,
                'msg'=>'删除失败',
            ];
        }
        return $data;
    }

    //品牌停用
    public function poststop()
    {
        $request=$this->request;
        $res=$request->param();
        $state=1;
        $res['state']=$state;
        unset($res['action']);
        $result=Db::table('tp_brand')->where('id',$res['id'])->update(['state'=>'1']);
        if($result){
            $data=
                [
                    'status'=>0,
                    'msg'=>'已停用',
                    'id'=>$res['id'],
                ];
        }else{
            $data=
                [
                    'status'=>1,
                    'msg'=>'出现未知错误，请稍后重试',
                    'id'=>$res['id'],
                ];
        }
        return json($data);
    }
    //品牌启用
    public function poststart()
    {
        $request=$this->request;
        $res=$request->param();
        $state=0;
        $res['state']=$state;
        unset($res['action']);
        $result=Db::table('tp_brand')->where('id',$res['id'])->update(['state'=>'0']);
        if($result){
            $data=
                [
                    'status'=>0,
                    'msg'=>'已启用',
                    'id'=>$res['id'],
                ];
        }else{
            $data=
                [
                    'status'=>1,
                    'msg'=>'出现未知错误，请稍后重试',
                    'id'=>$res['id'],
                ];
        }

        return json($data);
    }

    //品牌批量删除
    public function postdatadel()
    {
        $request=$this->request;
        $res=$request->param();
        $ids=explode(',',$res['ids']);
        $paths=array();
        Db::startTrans();
        try{
            foreach ($ids as $id)
            {
                $ress=Db::table('tp_brand')->where('id',$id)->find();
                //新建数组，用于储存图片
                $pathes='./uploads/brand_pic/'.$ress['brand_pic'];
                array_push($paths,$pathes);
                $result=Db::table('tp_brand')->where('id',$id)->delete();
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }

        if($result)
        {
            foreach ($paths as $pathinfos)
            {
                unlink($pathinfos);
                $pathess=pathinfo($pathinfos,PATHINFO_DIRNAME);
                if(count(scandir($pathess))==2)
                {
                    rmdir($pathess);
                }

            }
            $data=[
                'status'=>1,
                'msg'=>'批量删除成功',
            ];
        }else{
            $data=[
                'status'=>0,
                'msg'=>'批量删除失败',
            ];
        }
        return $data;
    }
}
?>